﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace PriceCuttingWar
{
	public partial class UIProgress : UserControl
	{
        public UIProgress()
        {
            // 为初始化变量所必需
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(UIProgress_Loaded);

            dt.Interval = TimeSpan.FromSeconds(1);
            dt.Tick += new EventHandler(dt_Tick);
            GSystem.UIProgress = this;
            this.CacheMode = new BitmapCache();
        }
        DispatcherTimer dt = new DispatcherTimer();
        public void TimeStart()
        {
            dt.Start();
            Second = MaxSecond;
        }
        public void TimeOver()
        {
            dt.Stop();
            GSystem.UIEndMessageBox.ShowThisInterface(GSystem.Root.GameScore);
        }
        void dt_Tick(object sender, EventArgs e)
        {
            Second -= 1;
        }
        private double GuandaoWidth;
        void UIProgress_Loaded(object sender, RoutedEventArgs e)
        {
            GuandaoWidth = Guandao.Width;
        }

        private int _MaxSecond = 60;
        /// <summary>
        /// 最大秒数
        /// </summary>
        public int MaxSecond
        {
            get { return _MaxSecond; }
            set { _MaxSecond = value; }
        }
        private int _second;
        /// <summary>
        /// 秒数
        /// </summary>
        public int Second
        {
            get { return _second; }
            set
            {
                if (value < 0)
                {
                    TimeOver();
                    return;
                }
                else
                {
                    if (value > _MaxSecond)
                        value = _MaxSecond;
                }
                _second = value;
                double p = ((double)value) / ((double)_MaxSecond);
                Guandao.Width = p * GuandaoWidth;
                TimeSum.Text = _second.ToString();
            }
        }
	}
}